// Created by carolsail

import { ENUM_AUDIO_CLIP, ENUM_GAME_MODE, ENUM_GAME_STATUS, ENUM_UI_TYPE } from "../Enum";
import Dice from "../game/Dice";
import AudioManager from "../manager/AudioManager";
import DataManager from "../manager/DataManager";
import PoolManager from "../manager/PoolManager";
import ResourceManager from "../manager/ResourceManager";
import { StaticInstance } from "../StaticInstance";
import { wxAvatar } from "../Utils";
import BaseLayer from "./Baselayer";

const { ccclass, property } = cc._decorator;

@ccclass
export default class MainLayer extends BaseLayer {

    btnSetting: cc.Node = null
    btnChat: cc.Node = null

    boards: cc.Node[] = []
    dice: cc.Node = null
    tip: cc.Node = null
    emoji: cc.Node = null


    onLoad() {
        this.btnSetting = cc.find('btn_setting', this.node)
        this.btnSetting.on('click', this.onSettingClick, this)
        this.btnChat = cc.find('btn_chat', this.node)
        this.btnChat.on('click', this.onChatClick, this)
        const boardRed = cc.find('main/red', this.node)
        const boardYellow = cc.find('main/yellow', this.node)
        const boardBlue = cc.find('main/blue', this.node)
        const boardGreen = cc.find('main/green', this.node)
        this.boards = [boardRed, boardYellow, boardBlue, boardGreen]
        this.dice = PoolManager.instance.getNode('Dice')
        this.tip = cc.find('main/tip', this.node)
        this.emoji = cc.find('main/emoji', this.node)
    }

    onDestroy() {
        this.btnSetting.off('click', this.onSettingClick, this)
        this.btnChat.off('click', this.onChatClick, this)
    }

    onEnable() { }

    onDisable() { }

    onSettingClick() {
        if (DataManager.instance.status == ENUM_GAME_STATUS.UNRUNING) return
        AudioManager.instance.playSound(ENUM_AUDIO_CLIP.CLICK)
        StaticInstance.uiManager.toggle(ENUM_UI_TYPE.SETTING_MAIN)
    }

    onChatClick() {
        AudioManager.instance.playSound(ENUM_AUDIO_CLIP.CLICK)
        StaticInstance.uiManager.toggle(ENUM_UI_TYPE.CHAT)
    }

    setUser() {
        let players = null
        if (DataManager.instance.room) players = DataManager.instance.room.players
        for (let i = 0; i < DataManager.instance.posData.length; i++) {
            const data = DataManager.instance.posData[i]
            const avatar = this.boards[i].getChildByName('avatar')
            const info = this.boards[i].getChildByName('info')
            if (data.length > 0) {
                let avatarName = '222'
                let nickname = '玩家'
                if (DataManager.instance.isAi[i]) {
                    avatarName = '111'
                    nickname = 'BOT'
                }
                avatar.getComponent(cc.Sprite).spriteFrame = ResourceManager.instance.getSprite(avatarName)
                info.getChildByName('nickname').getComponent(cc.Label).string = nickname
                // 授权信息
                if (DataManager.instance.mode == ENUM_GAME_MODE.MULTIPLE) {
                    if (players) {
                        const player = players[i]
                        if (player) {
                            info.getChildByName('nickname').getComponent(cc.Label).string = player.nickname
                            if (player.avatar) {
                                wxAvatar(player.avatar).then((texture: any) => {
                                    avatar.getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(texture)
                                })
                            }
                        }
                    }
                }
            } else {
                avatar.getComponent(cc.Sprite).spriteFrame = null
                info.getChildByName('nickname').getComponent(cc.Label).string = '空位'
            }
        }
    }

    getDice() {
        return this.dice.getComponent(Dice)
    }

    setDice() {
        const board = this.boards[DataManager.instance.round]
        const diceBoard = board.getChildByName('dice_board')
        this.dice.setParent(diceBoard)
        this.dice.setPosition(0, 0)
        this.dice.getComponent(Dice).refresh()
    }

    setTip() {
        const arr = DataManager.instance.canFlyNum[DataManager.instance.canFlyIndex]
        const tip = arr.join(',')
        this.tip.getComponent(cc.Label).string = `起飞号码[${tip}]`
    }

    setNotice(index: number) {
        const board = this.boards[DataManager.instance.round]
        const notice = board.getChildByName('notice')
        notice.stopAllActions()
        const labels = notice.getChildByName('labels')
        labels.children.forEach((label, k) => { label.active = k == index })
        notice.opacity = 255
        const act1 = cc.delayTime(0.8)
        const act2 = cc.fadeOut(0.5)
        const act = cc.sequence(act1, act2)
        cc.tween(notice).then(act).start()
    }

    setKill(type: number) {
        const board = this.boards[type]
        const kill = board.getChildByName('kill')
        kill.getComponent(cc.Label).string = `击杀[${DataManager.instance.killData[type]}]`
    }

    setEmoji(emoji: string | cc.SpriteFrame) {
        let content = null
        if (typeof emoji == 'string') {
            content = this.emoji.children[0]
            const from = content.getChildByName('from')
            from.getComponent(cc.Label).string = `${DataManager.instance.roleString[DataManager.instance.round]}: `
            const msg = content.getChildByName('msg')
            msg.getComponent(cc.Label).string = `${emoji}`
        } else {
            content = this.emoji.children[1]
            const from = content.getChildByName('from').getChildByName('label')
            from.getComponent(cc.Label).string = `来自${DataManager.instance.roleString[DataManager.instance.round]}`
            const msg = content.getChildByName('msg')
            msg.getComponent(cc.Sprite).spriteFrame = emoji
        }
        content.stopAllActions()
        content.setPosition(cc.v2(0, 0))
        content.opacity = 255
        const act1 = cc.delayTime(0.8)
        const act2 = cc.fadeOut(0.5)
        const act3 = cc.moveTo(0.5, 0, 100)
        const act4 = cc.spawn(act2, act3)
        const act = cc.sequence(act1, act4)
        cc.tween(content).then(act).start()
    }
}
